package cn.bingo.yunsj.controller;

import cn.bingo.yunsj.common.exception.EmException;
import cn.bingo.yunsj.common.exception.OwnException;
import cn.bingo.yunsj.common.result.Response;
import cn.bingo.yunsj.controller.model.RpModelAndRule;
import cn.bingo.yunsj.service.RuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Api("规则控制器")
@Controller
@RequestMapping("/rule")
@CrossOrigin
public class RuleController {

    @Autowired
    private RuleService ruleService;

    @RequestMapping(value = "/ALL",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("获取所有规则信息")
    public Response getAll(){
        return ruleService.getAll();
    }

    /**
     * 增加规则得时候，要注意，一个贺卡名称只能对应一个贺卡规则，也就是它得贺卡规则不为null，就不要继续插入
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("增加规则")
    public Response add(RpModelAndRule rpModelAndRule){
        if (StringUtils.isEmpty(rpModelAndRule.getType())
                || StringUtils.isEmpty(rpModelAndRule.getRuleName())) {
            throw new OwnException(EmException.PARAM_EXCPTION);
        }
        return ruleService.add(rpModelAndRule);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("修改规则")
    public Response update(RpModelAndRule rpModelAndRule){
        if (StringUtils.isEmpty(rpModelAndRule.getRepTime())
                || StringUtils.isEmpty(rpModelAndRule.getRuleCc())
                || StringUtils.isEmpty(rpModelAndRule.getType())
                || StringUtils.isEmpty(rpModelAndRule.getSendTime())
                || StringUtils.isEmpty(rpModelAndRule.getWay())
                || StringUtils.isEmpty(rpModelAndRule.getRuleSender())
                || StringUtils.isEmpty(rpModelAndRule.getRuleName())) {
            throw new OwnException(EmException.PARAM_EXCPTION);
        }
        return ruleService.update(rpModelAndRule);
    }

    @RequestMapping(value = "/unbund",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("接除规则与模板得联系")
    public Response unbund(RpModelAndRule rpModelAndRule){
        // 需要model_name
        if(StringUtils.isEmpty(rpModelAndRule.getType())){
            throw new OwnException(EmException.PARAM_EXCPTION);
        }
        return ruleService.unbund(rpModelAndRule);
    }
}
